import java.util.Date;
import java.util.HashMap;

/**Class represents a course/lecture at university with all its basic information. Students enroll into Courses. 
 * 
 * @author Kurt Micheli
 * @author Gabriel Rauter
 * @author Maximilian Mulatz
 */
public class Course {

	private String lvanr = "";
	private String title = "";
	private Date enrollStart = null;
	private Date enrollEnd = null;
	private Date checkOutEnd = null;

	private HashMap<String, Student> enrolledStudents = null;

        /**Creates a new course at university with all its basic information.
         * 
         * @param lvanr The individual number as technical description for the course.
         * @param title The title of the course
         * @param enrollStart   The day from which on Students can enroll into the course.
         * @param enrollEnd The day when the period for Students to enroll into the course ends.
         * @param checkOutEnd   The last possible day for Students to leave the course.  
         */
	public Course(String lvanr, String title, Date enrollStart, Date enrollEnd, Date checkOutEnd) {
		this.lvanr = lvanr;
		this.title = title;
		this.enrollStart = enrollStart;
		this.enrollEnd = enrollEnd;
		this.checkOutEnd= checkOutEnd;
		this.enrolledStudents = new HashMap<String, Student>();
	}

	/**Enrolls a new Student into the course if the date is not too early or too late. Students cannot enroll double into a course.
         * 
         * @param student The Student that will be enrolled into the course.
         * @return True if the Student was sucessfully enrolled, false otherwise.
         */
        public boolean enroll(Student student) {
		if (this.enrollStart.before(new Date())	&& this.enrollEnd.after(new Date()) && !this.enrolledStudents.containsKey(student.getMatnr())) {
			enrolledStudents.put(student.getMatnr(), student);
			return true;		
		}
		return false;
	}

        /**Checks out a Student of the course, if the date is not too late. Student has to be enrolled into the course to be checked out.
         * 
         * @param student The Student that wants to leave the course.
         * @return True if the Student was sucessfully checked out of the course, false otherwise.
         */
	public boolean checkOut(Student student) {
		if (this.checkOutEnd.before(new Date())) {
			return false;
		}
		if (enrolledStudents.remove(student.getMatnr()) == null) {
			return false;
		}
		return true;
	}

        /**Returns the course's technical numeric description.
         * 
         * @return A String-representation of the course's numeric description.
         */
	public String getLvanr() {
		return this.lvanr;
	}

	/**Returns the name of the course. 
         * 
         * @return A String-representation of the course's name.
         */
        public String getTitle() {
		return this.title;
	}

        /**Returns the date from with on Students can enroll into the course.
         * 
         * @return A Date-representation of the date from with on Students can enroll into the course.
         */
	public Date getEnrollStart() {
		return this.enrollStart;
	}

        /**Returns the date from which on Students can no longer enroll into the course.
         * 
         * @return A Date-representation of the day from which on it is no longer possible to enroll into the course.
         */
	public Date getEnrollEnd() {
		return this.enrollEnd;
	}

        /**Returns the date from which on Students can no longer check out of the course before it officially starts.
         * 
         * @return A Date-representation of the date from which on students can no longer check out of the course.
         */
	public Date getCheckOutEnd() {
		return this.checkOutEnd;
	}

        /**Returns the number of Students who are currently enrolled into the course. 
         * 
         * @return A Integer-representation of the number of Students enrolled into the specified course.
         */
	public int getStudentsCount() {
		return this.enrolledStudents.size();
	}

	/**Returns an detailled overview of all Students currently enrolled into the course.
         * 
         * @return A HashMap containing all Students currently enrolled into the course. Student's Matrikelnummer as key. 
         */
        public HashMap<String, Student> getStudents() {
		return enrolledStudents;
	}

        /**Returns all basic information about the course formatted.
         * 
         * @return A String-representation of the course's basic information: number, name and dates.
         */
	public String toString() {
		return this.getLvanr() + " " + this.getTitle() + " "
				+ this.getEnrollStart() + " " + this.getEnrollEnd() + " "
				+ this.getCheckOutEnd() + " " + this.getStudentsCount();
	}
}
